package org.garden.id.generator.segment.entity;

import lombok.Data;
import java.util.concurrent.atomic.AtomicLong;

@Data
public class Segment {

    private long maxId;

    private long loadingId;

    private AtomicLong currentId;

    /**
     * 每次增量
     */
    private int delta;

    public Result nextId() {
        long id = currentId.addAndGet(delta);
        if (id > maxId) {
            return new Result(ResultCode.OVER, id);
        }
        if (id >= loadingId) {
            return new Result(ResultCode.LOADING, id);
        }
        return new Result(ResultCode.NORMAL, id);
    }

    public boolean useful() {
        return currentId.get() <= maxId;
    }
}
